package com.dawn.framework.crypto.code.util;

import cn.hutool.json.JSONUtil;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Base64;

/**
 * @description:
 * @author: cnsu-cmh
 * @time: 2025/3/11 14:58
 */


public class AesUtil {
    // 注意：此处密钥需与前端保持一致（原始字符串，非 Hex 格式）
    private static final String SECRET_KEY = "VrnebKpyHTMfUiOjIxnRO0up8ADDtWbi";

    // 生成 16 字节 IV（取密钥前 16 字节）
    private static final byte[] KEY_BYTES = SECRET_KEY.getBytes(StandardCharsets.UTF_8);
    private static final byte[] IV_BYTES = SECRET_KEY.substring(0, 16).getBytes(StandardCharsets.UTF_8);

    /**
     * AES CBC 加密（与前端 CryptoJS 兼容）
     */
    public static String encrypt(String plainText) {
        try {
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            SecretKeySpec keySpec = new SecretKeySpec(KEY_BYTES, "AES");
            IvParameterSpec ivSpec = new IvParameterSpec(IV_BYTES);

            cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
            byte[] encrypted = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));
            return Base64.getEncoder().encodeToString(encrypted);
        } catch (Exception e) {
            throw new RuntimeException("加密失败", e);
        }
    }

    /**
     * AES CBC 解密（与前端 CryptoJS 兼容）
     */
    public static String decrypt(String encryptedText) {
        try {
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            SecretKeySpec keySpec = new SecretKeySpec(KEY_BYTES, "AES");
            IvParameterSpec ivSpec = new IvParameterSpec(IV_BYTES);

            cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
            byte[] decrypted = cipher.doFinal(Base64.getDecoder().decode(encryptedText));
            return new String(decrypted, StandardCharsets.UTF_8);
        } catch (Exception e) {
            throw new RuntimeException("解密失败", e);
        }
    }

    public static void main(String[] args) {

        String mw = "xQ46yFt4aTwyHKTKbBuruL67ahHxvyJeZxhLcyEV8tLhDrWtzsJYbeSJZxhdqW70KBpU0GZLDPWUuIjSiBwlrofRmp0uv5attEuJHvasZgSXpMaxnL4EyXRB9+JbVPl+rv8Iv5KLajTduRxrCXL5m+bWYqBEI/47+rLChkcpTNFxZ7mYuxVCvafMv8I0J3Cv9WYQsuHB3jSTFpFz7UIhs+C5AYUC42ZojtdPlRYcGbjzy+jY+u5zfHuhGSlON+JjAztsg4jGK9b5o2PNxhJoF9C007WWGwvIX4hZ9zYATeR1NRQHC+847AHa9Wzoy+53O5LTnTRn3wFXt+l+Ka+AVM0NNGw0BTaq++gA4GV/MlNGJNRdRIL23gWF2t+kjYbvWs9gbRlZiEudfvHjqLm0wSz8reasP81jBXM7JoYHzhhu816OwQQpxfLqF+7DOLl89qGzUl4G4z5IXQQQjl0RUcxrUdNwqTJ2JMtCKDwg0f4SiFjylSVDkX25xkNZY1H2bViy30kmDoscbDmDU8C4iuI8h1Qdk0QcVd4WxsNn+naz7lZF6kNASPsDMolvh7vYSlbfFlnVB/XREKQb2EloUkHN8Ququi3eyN2dZoSIf3xWDfvDZsNch+GR1Lt7rtAjdJ5JHoze7GefQCsL+dtxxfDqBxSdhnw5c9CoBK/5c07hB+cL7m8ZkFgxh2ROqOWw009U4tkIBv/tkN5XlKoF+K6pPqBCcpjflvcRp4qxQQNoxe149+DfE/cq0kBPvl4JhpjNvvgrAyOny/blW8Ldgg==";
        System.out.println("decrypt text: " + decrypt(mw));

        String s = JSONUtil.toJsonStr(mw);
        System.out.println(s);
    }
}
